package com.fa.service.impl;

import com.fa.common.annotation.Log;
import com.fa.common.utils.SecurityUtils;
import com.fa.common.utils.sign.RsaUtils;
import com.fa.domain.Warehouse;
import com.fa.domain.WarehouseSearchVO;
import com.fa.domain.WarehouseVO;
import com.fa.mapper.WarehouseMapper;
import com.fa.service.DingTalkAlarmService;
import com.fa.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private DingTalkAlarmService dingTalkAlarmService;

    @Override
    public List<WarehouseVO> warehouseList(WarehouseSearchVO warehouseSearchVO) {
        List<WarehouseVO> warehouseVOS = warehouseMapper.warehouseList(warehouseSearchVO);
        return warehouseVOS;
    }

    @Override
    public WarehouseVO getWarehouseById(Long warehouseId) {
        WarehouseVO warehouseById = warehouseMapper.getWarehouseById(warehouseId);
        return warehouseById;
    }

    @Override
    public void delWarehouse(List<Integer> warehouseIds) {
        warehouseMapper.delWarehouse(warehouseIds);
    }

    @Override
    public void updateWarehouse(Warehouse warehouse) {
        warehouse.setUpdateBy(SecurityUtils.getUsername());
        warehouse.setUpdateTime(new Date());
        warehouseMapper.updateWarehouse(warehouse);
    }

    @Log
    @Override
    public void addWarehouse(Warehouse warehouse) throws Exception {
        warehouse.setWarehouseCode(RsaUtils.decryptByPrivateKey(warehouse.getWarehouseCode()));
        warehouse.setCreateBy(SecurityUtils.getUsername());
        warehouse.setCreateTime(new Date());
        warehouseMapper.addWarehouse(warehouse);
        String msg = warehouse.getCreateBy() + "创建了" + warehouse.getWarehouseName();
        String alarmLevel = "一般";
        dingTalkAlarmService.addWarehouseAlare(msg,alarmLevel);
    }

    @Override
    public void addWarehouseList(List<Warehouse> warehouseList) {
        String username = SecurityUtils.getUsername();
        for (Warehouse warehouse : warehouseList) {
            warehouse.setCreateBy(username);
            warehouse.setCreateTime(new Date());
        }
        System.out.println(warehouseList);
        warehouseMapper.addWarehouseList(warehouseList);
        /*for (Warehouse warehouse : warehouseList) {
            warehouseMapper.addWarehouse(warehouse);
        }*/
    }
}
